package org.example.jsoup;

import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Demo1 {
    public static void main(String[] args) throws IOException {
////        String attr=" https://www.jiushujie.com/";
//        String attr="https://www.jiushujie.com/sell?465496540=&ajax=yw0&page=2";
//        URL url=new URL(attr);
//        Document doc = Jsoup.parse(url, 5000);
////        Elements elements = doc.select("div.book_list li>a");
//        Elements elements = doc.select("div.title>a");
////        System.out.println(elements.size());
//        for (Element element : elements) {
//            String href=element.attr("href");
//            String id=href.replaceAll("\\D","");
//            downBookInfo(id);
//        }
        //采集100本书
        int sum=0;
//        System.out.println(Math.ceil(100/12));
        for(int i=0;i<Math.ceil(100/12.0);i++){
            String attr="https://www.jiushujie.com/sell?465496540=&ajax=yw0&page="+i;
            if(i==0){
                attr="https://www.jiushujie.com/sell?465496540=&ajax=yw0";
            }
            URL url=new URL(attr);
            Document doc = Jsoup.parse(url, 5000);
//        Elements elements = doc.select("div.book_list li>a");
            Elements elements = doc.select("div.title>a");
//        System.out.println(elements.size());
            for (Element element : elements) {
                String href=element.attr("href");
                String id=href.replaceAll("\\D","");
                downBookInfo(id);
            }
            sum+=elements.size();
        }
        System.out.println(sum);
        //https://www.jiushujie.com/sell?465496540=&ajax=yw0
        //https://www.jiushujie.com/sell?465496540=&ajax=yw0&page=2
        //作业 请采集https://www.dygangs.net/ 每个分类20个数据
        //包括剧照和海报
    }
    public static void downBookInfo(String id) throws IOException {
        String attr=" https://www.jiushujie.com/sell/"+id;
//        System.out.println("attr = " + attr);
        URL url=new URL(attr);
        Map<String,String> book=new HashMap<>();
        Document doc = Jsoup.parse(url, 5000);
        if(doc.selectFirst("div.book_item div.info>h2")!=null){
            book.put("name",doc.selectFirst("div.book_item div.info>h2").text());
        }
        if(doc.selectFirst("div.book_item div.book_pic>img")!=null){
            book.put("image",doc.selectFirst("div.book_item div.book_pic>img").attr("src"));
        }
        if(doc.selectFirst("div.book_item div.detail")!=null){
            String detail=doc.selectFirst("div.book_item div.detail").text();
            detail.trim();
            String[] details = detail.split("[\\s\\n/]+");
            book.put("press",details[0]);
            book.put("author",details[1]);
            book.put("isbn",details[2]);
            book.put("price1",details[3]);
            book.put("price2",details[4]);
            book.put("old",details[5]);
            book.put("category",details[6]);
            if(doc.selectFirst("div.book_introduction_container")!=null){
                book.put("intro",doc.selectFirst("div.book_introduction_container").html());
            }
        }
        System.out.println("book="+book);
    }
    public static void main1(String[] args) throws IOException {
        String url=" https://www.vvic.com/apif/queryItemShopRecommended?city=gz ";
        URL u=new URL(url);
        URLConnection urlConnection = u.openConnection();
        InputStream inputStream = urlConnection.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        StringBuilder sb=new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            sb.append(line);
        }
        Gson gson=new Gson();
        LinkedTreeMap hashMap = gson.fromJson(sb.toString(), LinkedTreeMap.class);
        HashMap data = gson.fromJson(hashMap.get("data").toString(), HashMap.class);
        List cityMarkets=(List) data.get("cityMarkets");
        for (Object datum : cityMarkets) {
            System.out.println(datum);
        }
    }
}
